package com.ruix.app.widget;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ruix.app.R;

/**
 * Created by hadexs on 2017/4/16.
 */

public class CommonTitleBar extends RelativeLayout implements View.OnClickListener {

    public Context mContext;

    private OnTitleBarClickLeftListener leftListener;
    private OnTitleBarClickRightListener rightListener;

    private RelativeLayout mRlRootLayout;// 根布局－容器
    private ImageView mRlLeftBackBtn;// 左侧后退按钮
//    private ImageView mIvLeftBackBtn;// 左侧后退按钮图标
    private TextView mTvTitle;// 中间标题
    private TextView mTvRightBtn;// 右侧文字按钮

    public CommonTitleBar(Context context) {
        super(context);
        init(context);
    }

    public CommonTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        LayoutInflater.from(mContext).inflate(loadViewLayout(), this, true);// 加载布局文件
        initView();// 初始化控件
        initListener();// 设置监听器
    }

    public void setOnClickLeftListener(OnTitleBarClickLeftListener leftListener) {
        this.leftListener = leftListener;
    }

    public void setOnClickRightListener(OnTitleBarClickRightListener rightListener) {
        this.rightListener = rightListener;
    }


    // 加载布局文件
    private int loadViewLayout() {
        return R.layout.view_common_title_bar;
    }

    // 初始化控件
    private void initView() {
        mRlRootLayout = (RelativeLayout) findViewById(R.id.title_bar_root_layout);// 根布局－容器
        mRlLeftBackBtn = (ImageView) findViewById(R.id.btn_titile_bar_back);// 左侧后退按钮
//        mIvLeftBackBtn = (ImageView) findViewById(R.id.iv_left_back_btn);// 左侧后退按钮图标
        mTvTitle = (TextView) findViewById(R.id.tv_title_bar_title);// 中间标题
        mTvRightBtn = (TextView) findViewById(R.id.tv_titile_bar_right);// 右侧文字按钮
    }

    public void setRootLayoutBgColor(int colorResId) {
        mRlRootLayout.setBackgroundResource(colorResId);
    }

    public ImageView getLeftBackBtn() {
        return mRlLeftBackBtn;
    }

    public TextView getRightBtn() {
        return mTvRightBtn;
    }

    public void setLeftBackBtnVisible() {
        mRlLeftBackBtn.setVisibility(VISIBLE);
    }

    public void setLeftBackBtnGone() {
        mRlLeftBackBtn.setVisibility(GONE);
    }

//    public void setRightBtnVisible() {
//        mTvRightBtn.setVisibility(VISIBLE);
//    }
//
//    public void setRightBtnGone() {
//        mTvRightBtn.setVisibility(GONE);
//    }

    public void setTitle(String title) {
        mTvTitle.setText(title);
    }

    public void setTitle(int titleResId) {
        mTvTitle.setText(titleResId);
    }

//    // 设置左侧按钮图标
//    public void setLeftBtnIcon(int leftBtnIconResId) {
//        mIvLeftBackBtn.setBackgroundResource(leftBtnIconResId);
//    }

//    public void setRightBtnText(String rightBtnText) {
//        if (!TextUtils.isEmpty(rightBtnText)) {
//            mTvRightBtn.setText(rightBtnText);
//            setRightBtnVisible();
//        } else {
//            setRightBtnGone();
//        }
//    }

//    public void setRightBtnText(int rightBtnResId) {
//        if (rightBtnResId != 0) {
//            mTvRightBtn.setText(rightBtnResId);
//            setRightBtnVisible();
//        } else {
//            setRightBtnGone();
//        }
//    }

//    public void setRightBtnBg(int drawaleResId) {
//        mTvRightBtn.setBackgroundResource(drawaleResId);
//    }

    // 设置监听器
    private void initListener() {
        mRlLeftBackBtn.setOnClickListener(this);
        mTvRightBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_titile_bar_back:
                if (leftListener != null) {
                    leftListener.onClickLeftBackBtn();
                } else {
                    Activity activity = (Activity) getContext();
                    activity.finish();
                }
                break;
//            case R.id.tv_right_btn:
//                if (rightListener != null) {
//                    rightListener.onClickRightBtn();
//                }
//                break;
            default:
                break;
        }
    }


    public interface OnTitleBarClickLeftListener {
        void onClickLeftBackBtn();
    }

    public interface OnTitleBarClickRightListener {
        void onClickRightBtn();
    }
}